package lib.pacificapp.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import lib.pacificapp.model.HttpResp;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest; 
 
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames; 
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils; 

 
  

 
public class HttpUtils {

	 
	public static final int SUCCESS = 200;

	 
	public static final int NETWORK_ERR = -1;
 
	public static final int TIMEOUT = -2;

	 
	public static final int UNKNOWERROR = -3;
  
 
  private int timeOut=30 * 1000;
	public int getTimeOut() {
	return timeOut;
}


public void setTimeOut(int timeOut) {
	this.timeOut = timeOut;
}


	/**
	 * 褰撹姹傚畬鎴愬�
	 */
	public interface OnRequestExecuted {

		public void onRequestExecuted(int responseCode, String responseBody);
	}

	private static HttpUtils httpUtils;
 

	public HttpUtils() {

	}

	 
	public static HttpUtils getInstance() {
		if (null == httpUtils) {
			httpUtils = new HttpUtils();

		}
		return httpUtils;
	}
 
	 
	public HttpResp sendRequest(String requestUrl, List<NameValuePair>  data, String method,
			String charset, String authUserName, String authPassword) {
 
		DefaultHttpClient client = new DefaultHttpClient();

		client.getParams().setParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeOut());

		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
				getTimeOut());
 
		HttpUriRequest request = null;

	 
		HttpResponse response = null;

		int requestCode = HttpUtils.UNKNOWERROR;

	 
		String responseBody = null;

		try {

			if (authUserName != null && authPassword != null) {
 
				Credentials defaultcreds = new UsernamePasswordCredentials(
						authUserName, authPassword);

				client.getCredentialsProvider().setCredentials(AuthScope.ANY,
						defaultcreds);

			}

		 
			if ("post".equalsIgnoreCase(method)) {

				request = new HttpPost();
                
				((HttpPost) request).setURI(new URI(requestUrl));
 
//				((HttpPost) request).setHeader("Content-Type",
//						"application/json");

			//	StringEntity entity = new StringEntity(data, charset);
				   HttpEntity entity = new UrlEncodedFormEntity(data, HTTP.UTF_8);
          ((HttpPost) request).setEntity(entity);

			} else {
				// get鏂瑰紡璇锋眰
				request = new HttpGet();
               ((HttpGet) request).setURI(new URI(requestUrl));
			}

			if (request instanceof HttpGet) {

				 
				response = client.execute(((HttpGet) request));

			} else if (request instanceof HttpPost) {

				response = client.execute(((HttpPost) request));
			}

			// 瑙ｆ瀽璇锋眰缂�
			requestCode = response.getStatusLine().getStatusCode();

			responseBody = EntityUtils.toString(response.getEntity());
              
		} catch (MalformedURLException e) {
			// URL瑙ｆ瀽閿欒
			requestCode = HttpUtils.UNKNOWERROR;
			
		} catch (ProtocolException e) {
			// Tcp 閿欒�
			requestCode = HttpUtils.UNKNOWERROR;
		} catch (SocketTimeoutException e) {
			// 杩炴帴瓒呮椂鎿嶄�
			requestCode = HttpUtils.TIMEOUT;
		} catch (IOException e) {
			// IO 寮傚�
			 requestCode = HttpUtils.NETWORK_ERR;
			 e.printStackTrace();
		} catch (URISyntaxException e) {
			// URL瑙ｆ瀽閿欒
			requestCode = HttpUtils.UNKNOWERROR;
		} finally {
			// 娓呯悊璧勬簮
			client.getConnectionManager().shutdown();
		}
          return new HttpResp(requestCode,String.valueOf(responseBody));
		 
 

	}

	 

}
